﻿using System;
using System.Linq;
using System.Reflection;
using Df.Framework.Common;
using Df.Common.Forms;
using Df.Common.Model;
using Df.Business;

namespace Df.Business.Forms
{
    public class FormBusiness : BaseBusiness<DForm>, IFormBusiness
    {
        public DForm Get(int id = 0)
        {
            return base.Get(f => f.Id == id);
        }
        public IQueryable<DForm> Get(string refEntityName)
        {
            return base.GetList(x => x.RefEntityName == refEntityName);
        }
        public void Update(Df.Common.Forms.DForm DForm)
        {
            var dbForm = DataContext.Set<DForm>().Find(DForm.Id);
            base.Update(dbForm);
        }
        public void Update(int DFormId, Type refEntityType, int refEntityId)
        {
            var dbForm = DataContext.Set<DForm>().Find(DFormId);
            dbForm.RefEntityName = refEntityType.Name;
            dbForm.RefEntityId = refEntityId;
            base.Update(dbForm);
        }
        public void Update(int DFormId, string jsonElements)
        {
            base.UpdateProperty(DFormId, "JsonElements", jsonElements);
            base.Save();
        }

        //public void Create(Df.Common.Forms.DForm DForm, Type entityType, int refEntityId)
        //{
        //    DForm.RefEntityName = entityType.Name;
        //    DForm.RefEntityId = refEntityId;
        //    base.Create(DForm);
        //}

        public void Delete(int DFormId)
        {
            base.Delete(DFormId);
        }


        public void CreateEdit(DForm DForm, Type refType)
        {
            if (DForm.Id == 0)
            {
                Create(DForm);
            }
            else
            {
                base.Update(DForm);
            }
            var entity = DataContext.Set(refType).Find(DForm.RefEntityId);
            DataContext.Entry(entity).Property("DForm_Id").CurrentValue = DForm.Id;
            DataContext.SaveChanges();
            //dynamic bz = ServiceFactory.Get(refType);
            //bz.UpdateProperty(entityId, "DForm_Id", DForm.Id);
        }

        public new void Create(DForm DForm)
        {
            base.Create(DForm);
        }

        public void SectionFillTextBoxData(DfSection section, object data)
        {
            //var section = DForm.Elements.SingleOrDefault(e => e.Name == sectionName) as DfSection;
            var sectionProps = section.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var dataProps = data.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (var element in section.Elements)
            {
                var field = dataProps.SingleOrDefault(d => d.Name == element.Name);
                if (field != null)
                {
                    if (element is DfTextBox)
                    {
                        var val = dataProps.FirstOrDefault(d => d.Name == element.Name).GetValue(data);
                        if (val != null)
                            ((DfTextBox)element).Value = val.ToString();
                    }
                }
            }
        }
    }
}
